home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / ptv2n1.arc / L1.C < prev    next >
Text File  |  1991-03-26  |  866b  |  25 lines

  1. /* Finds and returns the greatest common divisor of two positive
  2.    integers. Works by trying every integral divisor between the
  3.    smaller of the two integers and 1, until a divisor that divides
  4.    both integers evenly is found. All C code tested with Microsoft
  5.    C 5.0 */
  6.  
  7. unsigned int gcd(unsigned int int1, unsigned int int2) {
  8.    unsigned int temp, trial_divisor;
  9.  
  10.    /* Swap if necessary to make sure that int1 >= int2 */
  11.    if (int1 < int2) {
  12.       temp = int1;
  13.       int1 = int2;
  14.       int2 = temp;
  15.    }
  16.  
  17.    /* Now just try every divisor from int2 on down, until a common
  18.       divisor is found. This can never be an infinite loop because
  19.       1 divides everything evenly */
  20.    for (trial_divisor = int2; ((int1 % trial_divisor) != 0) ||
  21.          ((int2 % trial_divisor) != 0); trial_divisor--)
  22.       ;
  23.    return(trial_divisor);
  24. }
  25.